define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'cms/user_chat_groups/index' + location.search,
                    add_url: 'cms/user_chat_groups/add',
                    edit_url: 'cms/user_chat_groups/edit',
                    del_url: 'cms/user_chat_groups/del',
                    multi_url: 'cms/user_chat_groups/multi',
                    import_url: 'cms/user_chat_groups/import',
                    table: 'user_chat_groups',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                search:false,//是否启用搜索框
                fixedColumns: true,//是否启用固定列功能
                fixedRightNumber: 1,//	右侧固定列的数量
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'),sortable: true,operate: false},
                        {field: 'name', title: __('群组名称'), operate: 'LIKE'},
                        {field: 'pic', title: __('头像'), events: Table.api.events.image, formatter: Table.api.formatter.image,
                            operate: false//用于查询时的操作符
                        },

                        {field: 'sort', title: __('排序')},


                        {
                            field: 'user_id', title: __('群主'), operate: false,
                            formatter: function (value, row) {
                                // 假设row.user是一个包含了用户信息的对象
                                return row.user ? row.user.nickname : '';
                            }
                        },
                        {field: 'group_user_count', title: __('群人数'),operate: false},

                        {
                            field: 'datalist', title: __('查看成员'), table: table, operate: false,
                            buttons: [
                                {
                                    name: 'content',
                                    text: __('查看成员'),
                                    classname: 'btn btn-xs btn-success btn-addtabs',
                                    icon: 'fa fa-file',
                                    url: 'cms/user_chat_group_users?chat_group_id={ids}'
                                }
                            ],
                            formatter: Table.api.formatter.buttons
                        },

                        {field: 'created_at', title: __('创建时间'),operate: false},

                        // 操作按钮
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
